#include "showimages.h"
#include "ui_showimages.h"
#include <QMessageBox>
#include "mainwindow.h"
#include <iostream>

extern double calculate_b(double a, int s);
extern double calculate_a(int s, double b);
extern int calculate_s(double b, double a);

ShowImages::ShowImages(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ShowImages)
{
    ui->setupUi(this);
    setWindowIcon(QIcon("icon.ico"));

    //fix_a_widget
    ui->fix_a_widget->xAxis->setRange(2, 100);
    ui->fix_a_widget->yAxis->setRange(0, 1);
    ui->fix_a_widget->xAxis->setLabel(tr("中继线数s"));
    ui->fix_a_widget->yAxis->setLabel(tr("时间阻塞率B"));
    ui->fix_a_widget->legend->setVisible(true);

    ui->fix_a_widget->addGraph();
    ui->fix_a_widget->addGraph();
    ui->fix_a_widget->addGraph();

    ui->fix_a_widget->graph(0)->setPen(QPen(Qt::blue));
    ui->fix_a_widget->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20)));

    ui->fix_a_widget->graph(1)->setPen(QPen(Qt::red));
    ui->fix_a_widget->graph(1)->setBrush(QBrush(QColor(0, 0, 255, 20)));

    ui->fix_a_widget->graph(2)->setPen(QPen(Qt::green));
    ui->fix_a_widget->graph(2)->setBrush(QBrush(QColor(0, 0, 255, 20)));

    ui->fix_a_widget->graph(0)->setName(QString("a1"));
    ui->fix_a_widget->graph(1)->setName(QString("a2"));
    ui->fix_a_widget->graph(2)->setName(QString("a3"));

    ui->fix_a_widget->graph(0)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(2)));
    ui->fix_a_widget->graph(1)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(4)));
    ui->fix_a_widget->graph(2)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(6)));

    //fix_b_widget
    ui->fix_b_widget->xAxis->setRange(6, 100);
    ui->fix_b_widget->yAxis->setRange(0, 180);
    ui->fix_b_widget->xAxis->setLabel(tr("到达呼叫量a"));
    ui->fix_b_widget->yAxis->setLabel(tr("中继线数s"));
    ui->fix_b_widget->legend->setVisible(true);

    ui->fix_b_widget->addGraph();
    ui->fix_b_widget->addGraph();
    ui->fix_b_widget->addGraph();

    ui->fix_b_widget->graph(0)->setPen(QPen(Qt::blue));
    ui->fix_b_widget->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20)));

    ui->fix_b_widget->graph(1)->setPen(QPen(Qt::red));
    ui->fix_b_widget->graph(1)->setBrush(QBrush(QColor(0, 0, 255, 20)));

    ui->fix_b_widget->graph(2)->setPen(QPen(Qt::green));
    ui->fix_b_widget->graph(2)->setBrush(QBrush(QColor(0, 0, 255, 20)));

    ui->fix_b_widget->graph(0)->setName(QString("B1"));
    ui->fix_b_widget->graph(1)->setName(QString("B2"));
    ui->fix_b_widget->graph(2)->setName(QString("B3"));

    ui->fix_b_widget->graph(0)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(2)));
    ui->fix_b_widget->graph(1)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(4)));
    ui->fix_b_widget->graph(2)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(6)));

    //fix_s_widget
    ui->fix_s_widget->xAxis->setRange(6, 150);
    ui->fix_s_widget->yAxis->setRange(0, 1);
    ui->fix_s_widget->xAxis->setLabel(tr("到达呼叫量a"));
    ui->fix_s_widget->yAxis->setLabel(tr("时间阻塞率B"));
    ui->fix_s_widget->legend->setVisible(true);

    ui->fix_s_widget->addGraph();
    ui->fix_s_widget->addGraph();
    ui->fix_s_widget->addGraph();

    ui->fix_s_widget->graph(0)->setPen(QPen(Qt::blue));
    ui->fix_s_widget->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20)));

    ui->fix_s_widget->graph(1)->setPen(QPen(Qt::red));
    ui->fix_s_widget->graph(1)->setBrush(QBrush(QColor(0, 0, 255, 20)));

    ui->fix_s_widget->graph(2)->setPen(QPen(Qt::green));
    ui->fix_s_widget->graph(2)->setBrush(QBrush(QColor(0, 0, 255, 20)));

    ui->fix_s_widget->graph(0)->setName(QString("s1"));
    ui->fix_s_widget->graph(1)->setName(QString("s2"));
    ui->fix_s_widget->graph(2)->setName(QString("s3"));

    ui->fix_s_widget->graph(0)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(2)));
    ui->fix_s_widget->graph(1)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(4)));
    ui->fix_s_widget->graph(2)->setScatterStyle(QCPScatterStyle((QCPScatterStyle::ScatterShape)(6)));

}

ShowImages::~ShowImages()
{
    delete ui;
}

void ShowImages::on_a_Show_Button_clicked()
{
    double a1 = ui->input_a_1->text().toDouble(),
            a2 = ui->input_a_2->text().toDouble(),
            a3 = ui->input_a_3->text().toDouble();
    if(a1<0.1 || a1>180 || a2<0.1 || a2>180 || a3<0.1 || a3>180)
    {
        QMessageBox::warning(this, "温馨提示", tr("请同时输入符合条件的a  0.1<a<180 "));
    } else {
        QVector<double> x1(50), x2(50), x3(50);
        QVector<double> in_s(50);

        for(int i=0; i<50; i++)
        {
            in_s[i] = (i+1)*2;
            x1[i] = calculate_b(a1, in_s[i]);
            x2[i] = calculate_b(a2, in_s[i]);
            x3[i] = calculate_b(a3, in_s[i]);
//            std::cout<<x1[i]<<std::endl;
        }

        ui->fix_a_widget->graph(0)->setData(in_s, x1);
        ui->fix_a_widget->graph(1)->setData(in_s, x2);
        ui->fix_a_widget->graph(2)->setData(in_s, x3);

        ui->fix_a_widget->replot();

    }
}

void ShowImages::on_b_Show_Button_clicked()
{
    double b1 = ui->input_b_1->text().toDouble(),
            b2 = ui->input_b_2->text().toDouble(),
            b3 = ui->input_b_3->text().toDouble();
    if(b1<0.0001 || b1>1 || b2<0.0001 || b2>1 || b3<0.0001 || b3>1)
    {
        QMessageBox::warning(this, "温馨提示", tr("请同时输入符合条件的B  0<B<1 "));
    } else {
        QVector<double> x1(50), x2(50), x3(50);
        QVector<double> in_a(50);

        for(int i=1; i<50; i++)
        {
            in_a[i] = (i+1)*3;
            x1[i] = calculate_s(b1, in_a[i]);
            x2[i] = calculate_s(b2, in_a[i]);
            x3[i] = calculate_s(b3, in_a[i]);
//            std::cout<<x1[i]<<std::endl;
        }

        ui->fix_b_widget->graph(0)->setData(in_a, x1);
        ui->fix_b_widget->graph(1)->setData(in_a, x2);
        ui->fix_b_widget->graph(2)->setData(in_a, x3);

        ui->fix_b_widget->replot();

    }
}

void ShowImages::on_s_Show_Button_clicked()
{
    double s1 = ui->input_s_1->text().toDouble(),
            s2 = ui->input_s_2->text().toDouble(),
            s3 = ui->input_s_3->text().toDouble();
    if(s1<1 || s1>180 || s2<1 || s2>180 || s3<1 || s3>180)
    {
        QMessageBox::warning(this, "温馨提示", tr("请同时输入符合条件的s  0<s<180 "));
    } else {
        QVector<double> x1(50), x2(50), x3(50);
        QVector<double> in_a(50);

        for(int i=1; i<50; i++)
        {
            in_a[i] = (i+1)*3;
            x1[i] = calculate_b(s1, in_a[i]);
            x2[i] = calculate_b(s2, in_a[i]);
            x3[i] = calculate_b(s3, in_a[i]);
//            std::cout<<x1[i]<<std::endl;
        }

        ui->fix_s_widget->graph(0)->setData(in_a, x1);
        ui->fix_s_widget->graph(1)->setData(in_a, x2);
        ui->fix_s_widget->graph(2)->setData(in_a, x3);

        ui->fix_s_widget->replot();

    }
}

